//
//  USColorPickerView.m
//

#import "USColorPickerView.h"

@interface USColorPickerView ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray<UIColor *> *colorList;
@property (nonatomic, assign) NSInteger selectedIndex;

@property (nonatomic, assign) CGFloat leftInset;

@end

@implementation USColorPickerView


- (instancetype)initWithFrame:(CGRect)frame andLeftLayout:(CGFloat)leftInset {
    if (self = [super initWithFrame:frame]) {
        _leftInset = leftInset;
        [self setupCollectionView];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.leftInset = 16;
        [self setupCollectionView];
    }
    return self;
}

- (void)setupCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 16;
    layout.minimumInteritemSpacing = 16;
    layout.sectionInset = UIEdgeInsetsMake(0, self.leftInset, 0, 16);

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.backgroundColor = UIColor.clearColor;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"colorCell"];
    
    [self addSubview:_collectionView];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.collectionView.frame = self.bounds;
}

- (void)setColorHexList:(NSArray<NSString *> *)hexList {
    NSMutableArray<UIColor *> *colors = [NSMutableArray array];
    for (NSString *hex in hexList) {
        UIColor *color = [self colorFromHexString:hex];
        if (color) {
            [colors addObject:color];
        }
    }
    self.colorList = colors;
    self.selectedIndex = 0;
    [self.collectionView reloadData];
}

#pragma mark - CollectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.colorList.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"colorCell" forIndexPath:indexPath];
    
    UIColor *color = self.colorList[indexPath.item];
    cell.contentView.backgroundColor = color;
    cell.contentView.layer.cornerRadius = 11; // 圆形效果
    cell.contentView.layer.masksToBounds = YES;
    
    if (indexPath.item == self.selectedIndex) {
        cell.contentView.layer.borderColor = UIColor.blackColor.CGColor;
        cell.contentView.layer.borderWidth = 2.f;
    } else {
        cell.contentView.layer.borderColor = UIColor.clearColor.CGColor;
        cell.contentView.layer.borderWidth = 0;
    }
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(22, 22);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [USFeedbackTool selectionChanged];
    self.selectedIndex = indexPath.item;
    [_collectionView reloadData];
    
    if (self.colorSelectedBlock) {
        self.colorSelectedBlock(self.colorList[indexPath.item]);
    }
}

#pragma mark - Helper

- (UIColor *)colorFromHexString:(NSString *)hexString {
    NSString *hex = [hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([hex hasPrefix:@"#"]) {
        hex = [hex substringFromIndex:1];
    }
    if (hex.length != 6) {
        return nil;
    }
    
    unsigned int r, g, b;
    NSScanner *scanner = [NSScanner scannerWithString:[hex substringWithRange:NSMakeRange(0, 2)]];
    [scanner scanHexInt:&r];
    scanner = [NSScanner scannerWithString:[hex substringWithRange:NSMakeRange(2, 2)]];
    [scanner scanHexInt:&g];
    scanner = [NSScanner scannerWithString:[hex substringWithRange:NSMakeRange(4, 2)]];
    [scanner scanHexInt:&b];
    return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0];
    
}

- (UIColor *)selectedColor {
    if (self.colorList.count > self.selectedIndex) {
        return self.colorList[self.selectedIndex];
    }
    return nil;
}

@end
